package com.example.translateappjava.DataBase;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class UserDbHelper extends SQLiteOpenHelper {
    private static UserDbHelper sHelper;
    private static final String DB_NAME = "user.db";   //数据库名
    private static final int VERSION = 4;    //版本号

    //必须实现其中一个构方法
    public UserDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    //创建单例，供使用调用该类里面的的增删改查的方法
    public synchronized static UserDbHelper getInstance(Context context) {
        if (null == sHelper) {
            sHelper = new UserDbHelper(context, DB_NAME, null, VERSION);
        }
        return sHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建user_table表
        db.execSQL("CREATE TABLE user_table(User_id integer PRIMARY KEY AUTOINCREMENT, " +
                "username text" +       //用户名
                ",password text" +      //密码
                ",nickname text" +      // 昵称
                ",sex text" +           //性别
                ",birth text" +         //生日
                ",name text" +          //姓名
                ",registerDate text" +  //注册时间
                ",phone_number text" +  //电话号码
                ",email text" +         //电子邮箱
                ",height text" +        //身高
                ",weight text" +        //体重
                ",sport_level text" +   //运动水平
                ",image blob" +         //图片
                ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion == 3) {
            // 为user_table添加新列
//            db.execSQL("ALTER TABLE user_table ADD COLUMN phone_number TEXT");
//            db.execSQL("ALTER TABLE user_table ADD COLUMN email TEXT");
//            db.execSQL("ALTER TABLE user_table ADD COLUMN height TEXT");
//            db.execSQL("ALTER TABLE user_table ADD COLUMN weight TEXT");
//            db.execSQL("ALTER TABLE user_table ADD COLUMN sport_level TEXT");
            db.execSQL("ALTER TABLE user_table ADD COLUMN image BLOB");
            // ... 这里可以添加更多的列

            // 如果有其他表需要更新，也可以在这里添加相应的SQL语句
        }
        // 如果将来有更多版本更新，可以继续添加if语句来处理
    }



    //TODO 在这里根据自己的业务需求，编写增删改查的方法，如下所示
//注册的逻辑
    public int register(String username, String password, String nickname, String sex, String birth, String name, String registerDate) {
        //获取SQLiteDatabase实例
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        //填充占位符
        values.put("username", username);
        values.put("password", password);
        values.put("nickname", nickname);
        values.put("sex", sex);
        values.put("birth", birth);
        values.put("name", name);
        values.put("registerDate", registerDate);
//        String nullColumnHack = "values(null,?,?,?)";
        //执行
        int insert = (int) db.insert("user_table", null, values);
//        db.close();
        return insert;
    }


    /**
     * 登录  根据用户名查找用户
     */
    @SuppressLint("Range")
    public UserInfo login(String username) {
        //获取SQLiteDatabase实例
        SQLiteDatabase db = getReadableDatabase();
        UserInfo userInfo = null;
        String sql = "SELECT *  FROM user_table WHERE username=?";
        String[] selectionArgs = {username};
        Cursor cursor = db.rawQuery(sql, selectionArgs);
        if (cursor.moveToNext()) {
            int user_id = cursor.getInt(cursor.getColumnIndex("User_id"));
            String username2 = cursor.getString(cursor.getColumnIndex("username"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            String nickname = cursor.getString(cursor.getColumnIndex("nickname"));
            String sex = cursor.getString(cursor.getColumnIndex("sex"));
            String birth = cursor.getString(cursor.getColumnIndex("birth"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String registerDate = cursor.getString(cursor.getColumnIndex("registerDate"));
            String phoneNumber = cursor.getString(cursor.getColumnIndex("phone_number"));
            String email = cursor.getString(cursor.getColumnIndex("email"));
            String height = cursor.getString(cursor.getColumnIndex("height"));
            String weight = cursor.getString(cursor.getColumnIndex("weight"));
            String sportLevel = cursor.getString(cursor.getColumnIndex("sport_level"));
            byte[] image = cursor.getBlob(cursor.getColumnIndex("image"));


            userInfo = new UserInfo(user_id, username2, password,  sex, name, nickname, birth, registerDate, phoneNumber, email, height, weight, sportLevel,image);
        }
        cursor.close();
        db.close();
        //++++++++++++++
        return userInfo;
    }

    public int updateUserInfo(UserInfo userInfo) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", userInfo.getName());
        values.put("birth", userInfo.getBirth());
        values.put("height", userInfo.getHeight());
        values.put("weight", userInfo.getWeight());
        values.put("sex", userInfo.getSex());
        values.put("email", userInfo.getEmail());
        values.put("phone_number", userInfo.getPhoneNumber());
        values.put("sport_level", userInfo.getSportLevel());
        values.put("image",userInfo.getImage());

        // 更新数据库
        return db.update("user_table", values, "User_id = ?", new String[]{String.valueOf(userInfo.getUserid())});
    }

    public boolean checkUsernameExists(String username) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query("user_table", new String[]{"username"}, "username = ?", new String[]{username}, null, null, null);
        boolean exists = cursor.getCount() > 0;
        cursor.close();
        return exists;
    }



}